package base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import Domain.News;

public class FirstApp {
	
	public static void main(String[] args) throws IOException {
		StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
		
		Metadata build = new MetadataSources(registry).getMetadataBuilder()
				.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE).build();
		
		SessionFactory sessionFactory = build.getSessionFactoryBuilder().build();
		
		Session session = sessionFactory.openSession();
		
		Transaction transaction = session.beginTransaction();
		
		News news = new News("tom", "dgw");
		InputStream stream = null;
		try {
			stream = new FileInputStream("Hydrangeas.jpg");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Blob blob = Hibernate.getLobCreator(session).createBlob(stream, stream.available());
		news.setImage(blob);
		session.save(news);
		
		transaction.commit();
		
		session.close();
		sessionFactory.close();
		
	}

}
